********Clean and compile data needed for analysis from MxFLS2*********

***Clean Book C, which contains individuals' roster and education information
cd "$mxfls2rawdata/hh05dta_bc"
use c_ls, clear
duplicates drop folio pid_link, force
destring ls, replace
egen hhsize=max(ls), by(folio)
keep folio pid_link hhsize ls04 ls14

*Information in location
merge m:1 folio using c_portad, gen(_mergecportad) keepus(mpio ent)

tempfile bookC_cleanmex
save "`bookC_cleanmex'", replace

***Clean Book 3B, which contains risk preference questions, smoking
cd "$mxfls2rawdata/hh05dta_b3b"
use iiib_portad.dta 

*Information on date of interview
preserve
use iiib_conpor, clear
keep if secuencia==1 // first visit
duplicates drop folio pid_link secuencia, force
tempfile b3b1
save "`b3b1'"
restore

merge 1:1 folio pid_link using "`b3b1'", gen(_mergeconpor)

*Attentiveness 
merge 1:1 folio pid_link using iiib_ne.dta, gen(_mergene) keepus(ne02 ne03)

rename ne02 b3bne2
rename ne03 b3bne3

*Smoking behavior
merge 1:1 folio pid_link using iiib_gh.dta, gen(_mergegh) keepus(gh06 gh09_1 gh10_1 gh10_21 gh10_22)

*Risk questions
merge 1:1 folio pid_link using iiib_rg.dta, gen(_mergerg) keepus(rg*)

tempfile book3b_cleanmex
save "`book3b_cleanmex'", replace

***Clean Book 3A, which contains work and violence experiences
cd "$mxfls2rawdata/hh05dta_b3a"
use iiia_portad.dta, clear

*Work information
merge 1:1 folio pid_link using  iiia_tb.dta, gen(_mergetb) keepus(tb02_1 tb03 tb04 tb05 tb24_26p_scian tb32p)

*Violence experiences
merge 1:1 folio pid_link using iiia_vli.dta, gen(_mergevli) keepus(vli01 vli02 vli05 vli06)

tempfile book3a_cleanmex
save "`book3a_cleanmex'", replace

***Clean Book EA, which contains individuals' cognitive measures

cd "$mxfls2rawdata/hh05dta_bea"

use ea_eca, clear

gen correctans1=8
gen correctans2=4
gen correctans3=5
gen correctans4=1
gen correctans5=2
gen correctans6=5
gen correctans7=6
gen correctans8=3
gen correctans9=7
gen correctans10=8
gen correctans11=7
gen correctans12=6

forval num=1/12 {
if `num'<=9 {
local typ 0`num'
} 
if `num'>=10 {
local typ `num'
} 
gen correct`typ'=.
replace correct`typ'=1 if eca`typ'==correctans`num' & eca`typ'!=.
replace correct`typ'=0 if eca`typ'!=correctans`num' & eca`typ'!=.

}

egen totcogscore=rowtotal(correct??), missing

keep folio pid_link totcogscore

tempfile bookea_cleanmex
save "`bookea_cleanmex'", replace

***Clean Book 2, which contains HH's experience of events/natural disasters/violence

cd "$mxfls2rawdata/hh05dta_b2"
use ii_portad.dta 

*Natural disasters/shocks experienced by HH
merge 1:1 folio using ii_se.dta, gen(_mergese) keepus(se01d)

*Crime HH (this only has how many times incident happened. for specific incidents details, see vlh2)
merge 1:1 folio using ii_vlh.dta, gen(_mergevlh) keepus(vlh04 vlh10a vlh10b vlh10c vlh11a vlh11b vlh11c)

tempfile book2_cleanmex
save "`book2_cleanmex'", replace

***HH income, assets, etc
cd "$reploc/programs"
do clean_mxfls2_balsheet.do
tempfile hhbalsheetfullmex
save "`hhbalsheetfullmex'", replace

***Merge all books/information

*Individual
use "`bookC_cleanmex'", clear
merge 1:1 folio pid_link using "`book3a_cleanmex'",  gen(_mergeb3a)
merge 1:1 folio pid_link using "`book3b_cleanmex'",  gen(_mergeb3b)
merge 1:1 folio pid_link using "`bookea_cleanmex'",  gen(_mergeea)

tempfile individualdatamex
save "`individualdatamex'", replace

*Household
use "`book2_cleanmex'", clear
merge m:1 folio using "`hhbalsheetfullmex'", gen(_mergebalsheet)
drop if _mergebalsheet!=3

tempfile hhdatamex
save "`hhdatamex'"


*Merge individual and HH
use "`individualdatamex'", clear

*Drop duplicates
sort pid_link
quietly by pid_link:  gen dup = cond(_N==1,0,_n)
drop if dup!=0

merge m:1 folio using "`hhdatamex'", gen(_mergehh)
drop if _mergehh!=3

*Rename variables to know they are MxFLS2 variables, except for pidlink, which is unique individual identifier across waves
foreach x of varlist _all {
if "`x'"!="pid_link" {
	rename `x' mxfls2_`x'
}
}

gen mxfls=2
